/**
 * Copyright (c) 2017, Alex. All rights reserved.
 */
package com.oschina.bottle.sample.web.web.interceptor;

import com.oshina.bottle.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局拦截器（默认拦截所有请求）
 *
 * ---- 打印请求日志
 *
 * @author <a href="mailto:zhongchao@gegejia.com">zhong</a>
 * @version 1.0 2017/10/11
 * @since 1.0
 */
@Component
public class GlobalInterceptor extends HandlerInterceptorAdapter
{
    public final static Logger logger = LoggerFactory.getLogger(GlobalInterceptor.class);

    @Value("${spring.profiles.active}")
    private String env;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception
    {
        //1. 记录请求日志
        logRequest(request);

        return true;
    }


    private void logRequest(HttpServletRequest request)
    {
        if (!"prod".equals(env)) {
            logger.info("请求地址: " + request.getRequestURI() + ", 参数: " + HttpUtils.getParam(request));
        }
    }
}
